package com.LeetCode.Array;


import org.junit.Test;

import java.util.*;

/**
 * 最长连续子序列
 */
public class LongestConsecutive {

    public int longestConsecutive(int[] nums) {
        Set<Integer> set = new HashSet<>();
        for(int num : nums){
            set.add(num);
        }

        int LongLen = 0;

        for(int num : nums){
            if(!set.contains(num-1)){
                int cur = num;
                int curLen = 1;

                while(set.contains(cur+1)){
                    curLen++;
                    cur++;
                }

                LongLen = Math.max(LongLen, curLen);
            }
        }
        return LongLen;
    }

    @Test
    public void test(){
        int[] arr = {100,1,2,3,200,4};
        System.out.println(longestConsecutive(arr));
    }
}
